package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
//import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;


    /**
     * 图片上传
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
    if (multipartFile==null||multipartFile.getSize()==0){
       return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
        String filename = multipartFile.getOriginalFilename();
        //去掉字符串的前i个字符/获取指定字符或者字符串最后一次出现的位置
        String suffix = filename.substring(filename.lastIndexOf("."));
        String UUidname = UUID.randomUUID().toString().replace("-", "") + suffix;
        String path=null;
        try {
         path = fileStorageService.uploadHtmlFile("", UUidname, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(path);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setType((short)0);
        wmMaterial.setIsCollection((short)0);

        save(wmMaterial);

        return ResponseResult.okResult(wmMaterial);

    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {

        dto.checkParam();

        IPage iPage=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        wrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        page(iPage,wrapper);

        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)iPage.getTotal());
        result.setData(iPage.getRecords());


        return result;
    }

}